<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Resources\Api\Admin\AdminRoleRouteResource;
use Illuminate\Http\Request;
use App\Http\Requests\Api\Admin\AdminRouteUpdateRequest;
use App\Http\Requests\Api\Admin\AdminRouteSetRoleRequest;
use App\Http\Resources\Api\Admin\AdminRouteResource;
use App\Models\Admin\AdminRoute;
use App\Models\Admin\AdminRouteRole;
use App\Models\Admin\AdminRole;
use App\Models\Admin\AdminPowerRoute;
use Illuminate\Support\Facades\Auth;
use App\Jobs\Api\AddRouteJob;
use App\Http\Queries\AdminRouteQuery;
use App\Http\Controllers\Api\Controller;

class RouteManageController extends Controller
{
    /**
     * 得到所有的路由
     */
    public function routes()
    {
        $routes = AdminRoute::with('roles')->get();
        $res = AdminRouteResource::collection($routes);
        return $this->success($res);
    }


    //返回路由列表
    public function index(AdminRouteQuery $query){
        //3个用户为一页
        $routes = $query->paginate($this->getPageSize());

        return AdminRouteResource::collection($routes);
    }

    /**
     * 添加路由
     * @param Request $request
     * @return mixed
     */
    public function addRoutes(Request $request)
    {
        $routesStr = $request->input('addRoutes');
        if ($routesStr){
            $routes = json_decode($routesStr, true);
            foreach ($routes as $route){
                AddRouteJob::dispatch(AdminRoute::class, $route);
            }
           return $this->success('添加路由成功');
        }
        return $this->failed('添加路由失败');
    }

    /**
     * 根据角色id获取路由
     * @param Request $request
     * @return mixed
     */
    public function getRoute(Request $request)
    {
        $role_id = $request->input('role_id');
        if ($role_id){
            $roleRoutes = AdminRole::find($role_id)->routes;
            return $this->success(AdminRoleRouteResource::collection($roleRoutes));
        }

        return $this->failed('获取失败');
    }

    //更新路由
    public function update(AdminRouteUpdateRequest $request)
    {
        $route = AdminRoute::find($request->input('route_id'));
        $attributes = $request->only(['title', 'redirect', 'icon', 'hidden', 'always_show', 'no_cache', 'affix', 'breadcrumb']);
        if ($route){
            $route->update($attributes);
            return $this->success(new AdminRouteResource($route));
        }
        return $this->failed('更新失败');
    }

    public function delete(Request $request)
    {
        $route_id = $request->input('route_id');
        if (!$route_id){
            return $this->failed('删除路由失败!');
        }

        $route = AdminRoute::find($route_id);
        if ($route){
            $route->delete();
            AdminRouteRole::where(['route_id' => $route_id])->delete();
            AdminPowerRoute::where(['route_id' => $route_id])->delete();
            return $this->success('删除路由成功');
        }

        return $this->failed('删除路由失败！');
    }

    public function setRole(AdminRouteSetRoleRequest $request)
    {
        $route_id = $request->input('route_id');
        $role_id = $request->input('role_id');
        $status = $request->input('status');
        $role = AdminRole::find($role_id);
        $route = AdminRoute::find($route_id);

        if ($role && $route){
            $data = ['role_id' => $role_id, 'route_id' => $route_id];
            $routeRole = AdminRouteRole::where($data)->first();
            if ($routeRole){
                $status < 1 && $routeRole->delete();
            }else{
                $status > 0 && AdminRouteRole::create($data);
            }
            return $this->success('设置成功');
        }else{
            return $this->failed('设置失败');
        }
    }
}
